package implementation.data.records;



/**
 * @author Maxim 
 * This class helps to enforce safety in concurrent environment.  
 * 
 */
public class ReadWriteGuard {
	
	
	protected int _activeReaders;
	protected int _activeWriters;
	protected int _waitingWriters;
	protected int _waitingReaders;
	
	
	/**
	 * class Constructor
	 */
	public ReadWriteGuard(){
		this._activeReaders=0;
		this._activeWriters=0;
		this._waitingReaders=0;
		this._waitingWriters=0;
	}

	
	/**
	 * 
	 * @return
	 * 			true if the current thread can read from the object.
	 */
	protected boolean allowReader(){
		return this._waitingWriters==0 & this._activeWriters==0;
	}
	
	/**
	 * 
	 * @return	
	 * 			true if the current thread can write to the object.
	 */
	protected boolean allowWriters(){
		return this._activeReaders==0 & this._activeWriters==0;
	}
	
}

